package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.heima.common.PageResult;
import com.heima.pojo.User;
import com.heima.pojo.dto.UserDto;
import com.heima.pojo.vo.SearchUserWorkVo;
import com.heima.pojo.vo.UserVo;
import com.heima.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 小甲鱼
 */
@RestController
@RequestMapping("/user-service/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/search")
    @ApiOperation("分页查询用户列表")
    public PageResult<UserVo> search(PageResult<UserVo> pageResult, @ApiParam(value = "用户名", required = false) String userName) {
        return userService.search(pageResult, userName);
    }

    @PostMapping
    @ApiOperation("添加用户")
    //TODO 用户密钥未添加
    public Boolean addUser(@RequestBody UserDto userDto) {
        if (ObjectUtil.isEmpty(userDto)) {
            return false;
        }
        return userService.addUser(userDto);
    }

    @PutMapping("/{userId}")
    @ApiOperation("修改用户")
    public Boolean updateUser(@PathVariable Integer userId, @RequestBody UserDto userDto) {
        if (ObjectUtil.isEmpty(userDto) || ObjectUtil.isEmpty(userId)) {
            return false;
        }
        userDto.setId(userId);
        return userService.updateById(BeanUtil.toBean(userDto, User.class));
    }

    @DeleteMapping("/{userId}")
    @ApiOperation("删除用户")
    public Boolean deleteUser(@PathVariable Integer userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return false;
        }
        return userService.removeById(userId);
    }

    @GetMapping("/repairerList/{innerCode}")
    @ApiOperation("获取维修人员列表")
    public List<Map<String, Object>> repairerList(@PathVariable String innerCode) {
        return userService.repairerList(innerCode);
    }

    @GetMapping("/operatorList/{innerCode}")
    @ApiOperation("获取操作人员列表")
    public List<Map<String, Object>> operatorList(@PathVariable String innerCode) {
        return userService.operatorList(innerCode);
    }

    @GetMapping("/searchUserWork")
    @ApiOperation("人员工作量列表")
    public PageResult<SearchUserWorkVo> searchUserWork(PageResult<SearchUserWorkVo> pageResult, String userName, Integer roleId, Boolean isRepair) {
        return userService.searchUserWork(pageResult, userName, roleId, isRepair);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取用户信息")
    public UserVo getUser(@PathVariable Integer id) {
        return userService.getUser(id);
    }
}
